﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using Newtonsoft.Json;
using System.Text;
using System.Data;


namespace ReportTracking.Services
{
    /// <summary>
    /// Summary description for PL2
    /// </summary>
    public class PL2 : IHttpHandler, IReadOnlySessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string sDVKD = context.Request.QueryString["dvkd"];
            string sfd = context.Request.QueryString["fromday"];
            string std = context.Request.QueryString["today"];
            int nSpeed = Convert.ToInt32(context.Request.QueryString["speed"]);
            int nReportType = Convert.ToInt32(context.Request.QueryString["reporttype"]);
            int page = Convert.ToInt32(context.Request.QueryString["page"]);
            int rows = Convert.ToInt32(context.Request.QueryString["rows"]);
            string sReload = HttpContext.Current.Request.Cookies["bRequest"].Value;
            string[] sArrDVDK = sDVKD.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
            int total = 0;
            StringBuilder sData = new StringBuilder();
            string sTable = "";
            sData.Append("{ ");
            sData.Append("total: ");
            /*if (sReload == "0")
            {
                context.Session["Data"] = null;
                if (sArrDVDK[0] == "All" || sArrDVDK.Length == 0)
                {
                    sTable = Newtonsoft.Json.JsonConvert.SerializeObject(GetData(sfd, std, ref total, nSpeed, (page - 1) * rows, rows));
                    
                }
                else
                {
                    //sTable = Newtonsoft.Json.JsonConvert.SerializeObject(GetData(sfd, std, ref total, nSpeed, sArrDVDK, (page - 1) * rows, rows));
                    
                }
                InfoReport inforeport = new InfoReport(sfd, std, sArrDVDK, total, nReportType, nSpeed.ToString());
                context.Session["InfoReprot"] = inforeport;
            }
            else
            {

            }*/
            sTable = Newtonsoft.Json.JsonConvert.SerializeObject(GetData(sfd, std, ref total, nSpeed, (page - 1) * rows, rows));
            sData.Append(total);
            sData.Append(",");
            sData.Append("\"rows\":");
            sData.Append(sTable);
            sData.Append("}");
            context.Response.Write(Newtonsoft.Json.JsonConvert.DeserializeObject(sData.ToString()));
        }
        private DataTable GetData(string sFromDay, string sToDay, ref  int total, int nSpeed, int nFromRecords, int nNumberRecords, int fromreocrds = 0, int torecords = 100)
        {
            DataTable tb = CreateDatatable();

            for (int i = 0; i < 50; i++)
            {
                tb.Rows.Add((i + 1), (i + 1), (i + 1), (i + 1), (i + 1), (i + 1), (i + 1), (i + 1), (i + 1), (i + 1), (i + 1), (i + 1), (i + 1), (i + 1), (i + 1), "6K1234 rrrf rrr r5");
            }

            total = tb.Rows.Count;
            if (tb != null)
            {
                IEnumerable<DataRow> results = (from myRow in tb.AsEnumerable().Skip(nFromRecords).Take(nNumberRecords)
                                                select myRow);
                return results.CopyToDataTable();
            }

            return null;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private static DataTable CreateDatatable()
        {
            DataTable tb = new DataTable();
            tb.Columns.Add("STT", typeof(int));
            tb.Columns.Add("platenumber", typeof(string));
            tb.Columns.Add("LHHD", typeof(string));
            tb.Columns.Add("DVQL", typeof(string));
            tb.Columns.Add("DVKD", typeof(string));
            tb.Columns.Add("HTTX", typeof(string));
            tb.Columns.Add("GPLX", typeof(string));
            tb.Columns.Add("NGAYHD", typeof(string));
            tb.Columns.Add("TKM", typeof(int));
            tb.Columns.Add("TL1", typeof(string));
            tb.Columns.Add("SL1", typeof(int));
            tb.Columns.Add("TL2", typeof(string));
            tb.Columns.Add("SL3", typeof(int));
            tb.Columns.Add("SL4", typeof(int));
            tb.Columns.Add("SL5", typeof(int));
            tb.Columns.Add("Note", typeof(string));
            tb.Dispose();
            return tb;

        }
    }
}